package cn.iocoder.yudao.hires.service.notice.impl;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.hires.entity.NoticeApplicationDO;
import cn.iocoder.yudao.hires.mapper.NoticeApplicationMapper;
import cn.iocoder.yudao.hires.service.notice.NoticeApplicationService;
import cn.iocoder.yudao.hires.vo.NoticeApplicationPageReqVO;
import cn.iocoder.yudao.hires.vo.PostTionShortVo;
import cn.iocoder.yudao.hires.vo.noticeapplication.NoticeApplicationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import cn.iocoder.yudao.position.entity.PositionDO;
import cn.iocoder.yudao.position.mapper.PositionMapper;
import cn.iocoder.yudao.position.service.PositionService;
import cn.iocoder.yudao.position.vo.PositionPageReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingyuv.captcha.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.NOT_EXISTS;

/**
 * 下游-投递记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class NoticeApplicationServiceImpl implements NoticeApplicationService {

    @Resource
    private NoticeApplicationMapper noticeApplicationMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private TenantMapper tenantMapper;

    @Override
    public String createNoticeApplication(NoticeApplicationSaveReqVO createReqVO) {

        //如果已经投递提示已经投递
        List<NoticeApplicationDO> noticeApplicationDOS = noticeApplicationMapper.selectList(new LambdaQueryWrapper<NoticeApplicationDO>().eq(NoticeApplicationDO::getUserId, createReqVO.getUserId()).eq(NoticeApplicationDO::getTenantId, createReqVO.getTenantId()).eq(NoticeApplicationDO::getPositionId, createReqVO.getPositionId()));
        if (CollUtil.isNotEmpty(noticeApplicationDOS)) {
            return "已经投递！";
        }
        // 插入
        NoticeApplicationDO noticeApplication = BeanUtils.toBean(createReqVO, NoticeApplicationDO.class);
        PostTionShortVo category = noticeApplicationMapper.getCategory(noticeApplication.getPositionId());
        noticeApplication.setPositionCategoryId(category.getCategoryId());
        noticeApplication.setPositionCategoryName(category.getCategoryName());
        noticeApplication.setApplicationTime(LocalDateTime.now());
        noticeApplicationMapper.insert(noticeApplication);
        // 返回
        return "投递成功";
    }

    @Override
    public void updateNoticeApplication(NoticeApplicationSaveReqVO updateReqVO) {
        // 校验存在
        validateNoticeApplicationExists(updateReqVO.getId());
        // 更新
        NoticeApplicationDO updateObj = BeanUtils.toBean(updateReqVO, NoticeApplicationDO.class);
        noticeApplicationMapper.updateById(updateObj);
    }

    @Override
    public void deleteNoticeApplication(Long id) {
        // 校验存在
        validateNoticeApplicationExists(id);
        // 删除
        noticeApplicationMapper.deleteById(id);
    }

    private void validateNoticeApplicationExists(Long id) {
        if (noticeApplicationMapper.selectById(id) == null) {
            throw exception(NOT_EXISTS);
        }
    }

    @Override
    public NoticeApplicationDO getNoticeApplication(Long id) {
        return noticeApplicationMapper.selectById(id);
    }

    @Override
    public PageResult<NoticeApplicationDO> getNoticeApplicationPage(NoticeApplicationPageReqVO pageReqVO) {
        return noticeApplicationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<NoticeApplicationDO> getNoticeApplicationMySelfList(Integer positionId, String positionName, Long tenantId, Long userId, Integer status) {
        return noticeApplicationMapper.getNoticeApplicationMySelfList(positionId, positionName, tenantId, userId, status);
    }

    @Override
    public String createNoticeApplicationList(NoticeApplicationSaveReqVO createReqVO) {
        List<PositionDO> positionDOS = positionMapper.selectList(new LambdaQueryWrapper<PositionDO>()
                .eq(PositionDO::getPositionCategoryId, createReqVO.getPositionCategoryId())
                .eq(PositionDO::getPositionCategoryName, createReqVO.getPositionCategoryName()));
        for (PositionDO positionDO : positionDOS) {
            Long tenantId = positionDO.getTenantId();
            TenantDO tenant = tenantMapper.getById(tenantId);

            //如果已经投递提示已经投递
            List<NoticeApplicationDO> noticeApplicationDOS = noticeApplicationMapper.selectList(new LambdaQueryWrapper<NoticeApplicationDO>()
                    .eq(NoticeApplicationDO::getUserId, createReqVO.getUserId())
                    .eq(NoticeApplicationDO::getPositionId, positionDO.getId()));
            if (CollUtil.isNotEmpty(noticeApplicationDOS)) {
                continue;
            }
            // 插入
            NoticeApplicationDO noticeApplication = BeanUtils.toBean(createReqVO, NoticeApplicationDO.class);
            noticeApplication.setApplicationTime(LocalDateTime.now());
            noticeApplication.setTenantId(tenantId);
            noticeApplication.setUserId(createReqVO.getUserId());
            noticeApplication.setTenantId(positionDO.getTenantId());
            noticeApplication.setCompanyName(tenant.getName());
            noticeApplication.setPositionId(positionDO.getId());
            noticeApplication.setPositionName(positionDO.getTitle());
            noticeApplication.setApplicationTime(LocalDateTime.now());
            noticeApplication.setStatus(0);
            noticeApplication.setIsRead(0);
            noticeApplication.setMark(0);
            noticeApplication.setSource(1);
            noticeApplication.setIsLatest(positionDO.getIsLatest());
            noticeApplication.setIsUrgent(positionDO.getIsUrgent());
            noticeApplication.setPositionCategoryId(createReqVO.getPositionCategoryId());
            noticeApplication.setPositionCategoryName(createReqVO.getPositionCategoryName());
            noticeApplication.setDeleted(false);
            noticeApplicationMapper.insert(noticeApplication);
            // 返回
        }
        return "投递成功";

    }

}